package com.seele.tigerwallet.ui.activtiy;

import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.seele.tigerwallet.R;
import com.seele.tigerwallet.ui.MnemonicActivity;
import com.seele.tigerwallet.ui.application.SeeleApplication;
import com.seele.tigerwallet.ui.base.BaseActivity;
import com.seele.tigerwallet.ui.utils.LoadWebViewManager;
import com.seele.tigerwallet.utils.EthWalletUtil;

import java.util.ArrayList;
import java.util.List;

public class ImportWalletActivity extends BaseActivity {

    private Button mBtnSelect;
    private Button mBtnImport;
    private PopupWindow mPopupWindow;
    private List<String> mnemonicList = new ArrayList<String>();



    @Override
    protected int getLayoutId() {
        return R.layout.mnemonic_import;
    }

    @Override
    protected void init() {
        iniView();
    }

    private void iniView() {
        mBtnSelect = (Button) findViewById(R.id.btn_import);
        mBtnImport = (Button) findViewById(R.id.btn_start_imort);


    }

    public void onBtnClick(View view) {
        if (view.getId() == R.id.btn_import) {
            showPopWindow();
        } else if (view.getId() == R.id.btn_start_imort) {
            onImport();
        }
    }

    private void onImport() {
        showLoadingDialog();
        mnemonicList.clear();
        for (int i = 0; i < 12; i++) {
            int textViewId = getResources().getIdentifier("et_show" + i, "id", ImportWalletActivity.this.getPackageName());
            EditText etText = (EditText) findViewById(textViewId);
            String text = etText.getText().toString();
            if (text == null ||text.trim().length() == 0) {
                dismissLoadingDialog();
                showAlertDialog(getString(R.string.input_mnemonic), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
                return;
            }
            mnemonicList.add(text.trim());
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                final boolean isOk = EthWalletUtil.checkMnemonic(mnemonicList);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dismissLoadingDialog();
                        if (isOk) {
                            Toast.makeText(ImportWalletActivity.this, getString(R.string.import_wallet_success), Toast.LENGTH_SHORT).show();
                            Bundle bundle = new Bundle();
                            bundle.putStringArrayList(KeyActivity.TAG, (ArrayList<String>) mnemonicList);
                            startActivity(KeyActivity.class, bundle);
                            finish();
                        } else {
                            showAlertDialog(getString(R.string.input_mnemonic), new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                }
                            });
                        }

                    }
                });
            }
        }).start();
    }


    public void onTextClick(View view) {
        if (view.getId() == R.id.tv_mnemonic_exp) {
            startActivity(MnemonicActivity.class);
        }
    }

    private void showPopWindow() {
        View contentView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.popupwindow, null);
        mPopupWindow = new PopupWindow(findViewById(R.id.popup_layout), mBtnSelect.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setAnimationStyle(R.style.AnimationTopFade);
        //ColorDrawable dw = new ColorDrawable(Color.parseColor("#B1B9CA"));
        ColorDrawable dw = new ColorDrawable(Color.WHITE);
        mPopupWindow.setBackgroundDrawable(dw);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        mPopupWindow.setContentView(contentView);

        ListView listView = (ListView) contentView.findViewById(R.id.list);
        final String [] walletList = new String[1];
        walletList[0] = getString(R.string.general);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, walletList);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mBtnSelect.setText(walletList[position]);
                mPopupWindow.dismiss();
                mBtnSelect.setBackgroundResource(R.drawable.mnemonic_show_shape);
            }
        });

        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                mBtnSelect.setBackgroundResource(R.drawable.mnemonic_show_shape);
            }
        });

        mPopupWindow.setFocusable(true);
        mBtnSelect.setBackgroundResource(R.drawable.mnemonic_show_shape_click);
        mPopupWindow.showAsDropDown(mBtnSelect);
    }


}
